package com.chat.demo.userservice;

import com.chat.demo.auth.annotation.JwtAuth;
import com.chat.demo.userservice.DTO.User.*;
import com.chat.demo.userservice.Entity.Entity.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class User_Controller {

    @Autowired
    private User_Service userService;

    @GetMapping("/findUserById")
    public User getUserByID(@RequestParam("id") @NotNull @Min(1) Long id) {
        return userService.getUserById(id);
    }

    @PostMapping("/register")
    public int registerUser(@RequestBody RegisterRequest request) {
        return userService.register(request);
    }

    @PostMapping("/login")
    public String authenticateUser(@RequestBody LoginRequest request) {
        return userService.login(request.getUsername(), request.getPassword());
    }

    @JwtAuth
    @GetMapping("/getUserInfo")
    public ResponseEntity<UserInfo> getUserInfo(HttpServletRequest request) {
        return userService.getUserInfoByToken(request);
    }

    @JwtAuth
    @GetMapping("/getUserOnlineStatus")
    public ResponseEntity<Boolean> getUserOnlineStatus(@RequestParam("id") @NotNull @Min(1) Long id) {
        return ResponseEntity.ok(userService.getUserOnlineStatus(id));
    }

    @JwtAuth
    @PostMapping("/updateUserInfo")
    public int updateUserInfo(@RequestBody CouldUpdateUserInfo userInfo, HttpServletRequest request) {
        return userService.updateUserInfo(userInfo, request);
    }

    @JwtAuth
    @PostMapping("/resetPassword")
    public Map<String, Object> resetPassword(@RequestBody PasswordResetRequest requestBody, HttpServletRequest request) {
        return userService.resetPassword(requestBody, request);
    }

    @JwtAuth
    @PostMapping("/logout")
    public Map<String, String> logout(HttpServletRequest request) {
        return userService.logout(request);
    }
}